﻿Imports System.IO
Public Class NotePadFile
    Public Sub AddNewFile(ByVal text As RichTextBox)
        text.Text = ""
    End Sub

    Public Function OpenFile1(ByRef open As OpenFileDialog)
        Dim myStream As Stream = Nothing
        Dim openFileDialog1 As New OpenFileDialog()

        open.InitialDirectory = "c:\"
        open.Filter = "Normal text file (*.txt)|*.txt|Active Server Pages scirpt file (*.asp)|*.asp|All files (*.*)|*.*"
        open.FilterIndex = 3
        open.RestoreDirectory = True

        If open.ShowDialog() = DialogResult.OK Then
            Try
                myStream = open.OpenFile()
                If (myStream IsNot Nothing) Then
                    ' Insert code to read the stream here. 
                    Dim streamreader = New StreamReader(myStream, System.Text.Encoding.UTF8)
                    Dim arr As String = streamreader.ReadToEnd()
                    Return arr
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
            Finally
                ' Check this again, since we need to make sure we didn't throw an exception on open. 
                If (myStream IsNot Nothing) Then
                    myStream.Close()
                End If
            End Try
        End If
        Return Nothing
    End Function

    'Public Sub OpenFile(ByVal text As TextBox, ByVal txtBody As TextBox, ByRef open As OpenFileDialog)
    '    open.ShowDialog()
    '    Dim objReader
    '    Try
    '        objReader = New System.IO.StreamReader(text.Text)
    '        txtBody.Text = objReader.ReadToEnd
    '        objReader.Close()
    '        ' already test
    '    Catch ex As Exception

    '    End Try

    'End Sub

    Public Sub SaveFile(ByRef save As SaveFileDialog, ByRef txtBody As RichTextBox)
        save.ShowDialog()
        Dim fileToSaveAs As String = save.FileName
        Try
            Dim objectwriter As New System.IO.StreamWriter(fileToSaveAs)
            objectwriter.Write(txtBody.Text)
            MsgBox("Your text has been saved!")
            objectwriter.Close()
        Catch ex As Exception
            MsgBox("You has been cancel!")
        End Try

        'Dim myStream As Stream = Nothing
        'Dim openFileDialog1 As New OpenFileDialog()
        'Dim fileToSaveAs As String = save.FileName

        'save.InitialDirectory = "c:\"
        'save.Filter = "Normal text file (*.txt)|*.txt|Active Server Pages scirpt file (*.asp)|*.asp|All files (*.*)|*.*"
        'save.FilterIndex = 2
        'save.RestoreDirectory = True

        'If save.ShowDialog() = DialogResult.OK Then
        '    MessageBox.Show(DialogResult.OK)
        '    Try
        '        'myStream = save.FileName
        '        If (myStream IsNot Nothing) Then
        '            ' Insert code to read the stream here. 
        '            'Dim streamwriter = New StreamWriter(myStream, System.Text.Encoding.UTF8)
        '            'streamwriter.Write(txtBody)
        '            Dim objectwriter As New System.IO.StreamWriter(fileToSaveAs)
        '            objectwriter.Write(txtBody.Text)
        '        End If
        '    Catch Ex As Exception
        '        MessageBox.Show("Cannot write file from disk. Original error: " & Ex.Message)
        '    Finally
        '        ' Check this again, since we need to make sure we didn't throw an exception on open. 
        '        If (myStream IsNot Nothing) Then
        '            myStream.Close()
        '        End If
        '    End Try
        'End If

    End Sub

    Public Function FindMyText(ByVal txtToSearch As String, ByVal searchStart As Integer, ByVal searchEnd As Integer, ByVal rtb As RichTextBox) As Integer
        Dim indexOfSearchText As Integer
        ' Unselect the previously searched string
        If searchStart > 0 AndAlso searchEnd > 0 AndAlso indexOfSearchText >= 0 Then
            rtb.Undo()
        End If

        ' Set the return value to -1 by default.
        Dim retVal As Integer = -1

        ' A valid starting index should be specified.
        ' if indexOfSearchText = -1, the end of search
        If searchStart >= 0 AndAlso indexOfSearchText >= 0 Then
            ' A valid ending index
            If searchEnd > searchStart OrElse searchEnd = -1 Then
                ' Find the position of search string in RichTextBox
                indexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None)
                ' Determine whether the text was found in richTextBox1.
                If indexOfSearchText <> -1 Then
                    ' Return the index to the specified search text.
                    retVal = indexOfSearchText
                End If
            End If
        End If
        Return retVal
    End Function
End Class
